﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShredTheLog.Api;
using ShredTheLog.Common.MVVM;
using System.ComponentModel.Composition;
using System.Diagnostics.Contracts;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace ShredTheLog.ViewModels
{
    [Export(typeof(ISettingsPageViewModel))]
    [OrderMetadata(Order = 200)]
    [PartCreationPolicy(CreationPolicy.NonShared)] // Модель должна пересоздаваться, чтобы после закрытия окна настроек по отмене окно настроек при повторном открытии отображало актуальные значения настроек, а не отмененные в ходе предыдущего сеанса
    public sealed class TranslatorSettingsPageViewModel : ViewModelBase, ISettingsPageViewModel
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        [Import]
        private ITranslatorSettingsSection TranslatorSettings { get; set; }

        [ImportMany]
        private Lazy<ITranslator, IOrderMetadata>[] _translators = null;
        public IEnumerable<ITranslator> Translators 
        {
            get 
            {
                Contract.Assume(_translators != null);
                return from i in _translators orderby i.Metadata.Order select i.Value; 
            }
        }

        public TranslatorSettingsPageViewModel()
        {
            IsActive = TranslatorSettings.IsActive;
            TranslatorID = TranslatorSettings.TranslatorID;
            IsUiLanguageTarget = TranslatorSettings.IsUiLanguageTarget;
            TargetLanguage = TranslatorSettings.TargetLanguage;
        }

        public string Header { get { return ShredServices.GetLocalizedString("s_Translation"); } }

        public bool IsActive 
        {
            get { return _isActive; }
            set { _isActive = value; OnPropertyChanged(() => IsActive); }
        }
        private bool _isActive;

        public string TranslatorID 
        {
            get { return _translatorID; }
            set 
            { 
                _translatorID = value;
                TargetLanguage = Language.English;
                OnPropertyChanged(() => SupportedTargetLanguages);
            }
        }
        private string _translatorID;

        public IEnumerable<Language> SupportedTargetLanguages 
        {
            get { return Translators.Single(i => i.ID == TranslatorID).SupportedLanguages; }
        }

        public bool IsUiLanguageTarget 
        {
            get { return _isUiLanguageTarget; }
            set { _isUiLanguageTarget = value; OnPropertyChanged(() => IsUiLanguageTarget); }
        }
        private bool _isUiLanguageTarget;

        public Language TargetLanguage 
        {
            get { return _targetLanguage; }
            set { _targetLanguage = value; OnPropertyChanged(() => TargetLanguage); }
        }
        private Language _targetLanguage;

        public void Save()
        {
            TranslatorSettings.IsActive = IsActive;
            TranslatorSettings.TranslatorID = TranslatorID;
            TranslatorSettings.IsUiLanguageTarget = IsUiLanguageTarget;
            TranslatorSettings.TargetLanguage = TargetLanguage;
        }
    }
}
